<?php
/**
 * 类别
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/6
 * Time: 22:34
 */

namespace app\admin\controller;


use app\common\controller\AdminBase;
use app\common\model\BoolStatus;
use app\common\model\Category as CategoryModel;
use \app\admin\validate\Category as CategoryValidate;

class Category extends AdminBase
{
    /**
     * 分类列表
     * @return mixed
     */
    public function index()
    {
        $categoryModel = new CategoryModel();
        $list          = $categoryModel->column('id, name, dis_order, status, updated_time, created_time, seo_title, seo_keywords, seo_desc, pid');

        $this->assign('list', generateTree($list));

        $status = BoolStatus::getLables();
        $this->assign('status', $status);
        return $this->fetch('category/index');
    }

    /**
     * 设置分类
     * @return mixed
     */
    public function set()
    {
        $id            = $this->request->param('id', 0);
        $categoryModel = new CategoryModel();
        if ($this->request->isPost()) {
            $name         = $this->request->param('name');
            $pid          = $this->request->param('pid');
            $status       = $this->request->param('status');
            $dis_order    = $this->request->param('dis_order');
            $seo_title    = $this->request->param('seo_title');
            $seo_keywords = $this->request->param('seo_keywords');
            $seo_desc     = $this->request->param('seo_desc');

            //验证数据
            $validate = new CategoryValidate();
            if (!$validate->check(['name' => $name, 'pid' => $pid, 'status' => $status, 'dis_order' => $dis_order])) {
                $this->error($validate->getError());
            }

            $currentTime = time();
            if (!$id) {
                //新增
                $count = $categoryModel->where('name', '=', $name)->count();
                if ($count) {
                    $this->error('该分类名称已存在，请输入其他的分类名称~~');
                }
                $categoryModel->created_time = $currentTime;
            } else {
                //编辑
                $count = $categoryModel->where('name', '=', $name)->whereNotIn('id', $id)->count();
                if ($count) {
                    $this->error('该分类名称已存在，请输入其他的分类名称~~');
                }
                $categoryModel = $categoryModel->find($id);
            }

            $categoryModel->name         = $name;
            $categoryModel->pid          = $pid;
            $categoryModel->updated_time = $currentTime;
            $categoryModel->dis_order    = $dis_order;
            $categoryModel->status       = $status;
            $categoryModel->seo_title    = $seo_title;
            $categoryModel->seo_keywords = $seo_keywords;
            $categoryModel->seo_desc     = $seo_desc;

            $result = $categoryModel->save();
            if ($result) {
                $this->success('操作成功~~', 'category/index');
            } else {
                $this->error('操作失败~~');
            }
        }

        $category = null;
        if ($id) {
            $category = $categoryModel::find($id);
        }
        $pids = $categoryModel->where('pid', '=', 0)->whereNotIn('id', $id)->field('id, name')->select();

        $this->assign('pids', $pids);
        $this->assign('category', $category);

        $status = BoolStatus::getLables();
        $this->assign('status', $status);
        return $this->fetch('category/set');
    }
}